﻿using UnityEngine;
using System.Collections;

[RequireComponent(typeof(BoxCollider))]
public class PlayerPhysics : MonoBehaviour {

	private Vector3 originalSize;
	private Vector3 originalCenter;
	private float colliderScale;
	public BoxCollider collider;
	public LayerMask collisionMask;

	public Vector2 s;
	public Vector2 c;

	private int collisionDivisionX = 3;
	private int collisionDivisionY = 10;
	[HideInInspector]
	public bool grounded = false;

	[HideInInspector]
	public bool stopedMove = false;

	private float space = .005f; //space between player and ground

	Ray ray; 
	RaycastHit hit;

	void Start()
	{
		collider = GetComponent<BoxCollider>();
		colliderScale = transform.localScale.x;
		originalSize = collider.size;
		originalCenter = collider.center;
		setCollider(originalSize, originalCenter);
	}

	public void movePlayer(Vector2 moveAmount)
	{
		float deltaY = moveAmount.y;
		float deltaX = moveAmount.x;
		Vector2 pPos = transform.position;
		grounded = false;
		//top, down collision
		for (int i = 0; i <collisionDivisionX; i++) 
		{
			float dir = Mathf.Sign(deltaY);
			float x = (pPos.x + c.x - s.x/2) +s.x/(collisionDivisionX -1)*i;
			float y = pPos.y + c.y + s.y/2 *dir;

			ray  = new Ray(new Vector2(x, y), new Vector2(0,dir));
			Debug.DrawRay(ray.origin,ray.direction);

			if (Physics.Raycast(ray,out hit,Mathf.Abs(deltaY)+ space,collisionMask))
			{
				float dst = Vector3.Distance(ray.origin, hit.point); //Distance between player and ground
				if(dst > space)
				{
					deltaY = dst * dir -space * dir;
				}
				else
				{
					deltaY = 0;
				}
				grounded = true;
				break;
			}
		}
		//Left and right collision
		stopedMove = false;
		for (int i = 0; i <collisionDivisionY; i++) 
		{
			float dir = Mathf.Sign(deltaX);
			float x = pPos.x + c.x + s.x/2*dir;
			float y = pPos.y + c.y -s.y/2 +s.y/(collisionDivisionY -1) *i;
			
			ray  = new Ray(new Vector2(x, y), new Vector2(dir,0));
			Debug.DrawRay(ray.origin,ray.direction);
			
			if (Physics.Raycast(ray,out hit,Mathf.Abs(deltaX)+ space,collisionMask))
			{
				float dst = Vector3.Distance(ray.origin, hit.point); //Distance between player and ground
				if(dst > space)
				{
					deltaX = dst * dir -space * dir;
				}
				else
				{
					deltaX = 0;
				}
				stopedMove = true;
				break;
			}
		}
		//wenn keine Collision erkannt, prüfe auf diagonale
		if (!grounded && !stopedMove) //matheshit für diagonale collision
		{
						Vector3 playerDir = new Vector3 (deltaX, deltaY);
						Vector3 origin = new Vector3 (pPos.x + c.x + s.x / 2 * Mathf.Sign (deltaX), pPos.y + c.y + s.y / 2 * Mathf.Sign (deltaY)); 
						ray = new Ray (origin, playerDir.normalized);

						if (Physics.Raycast (ray, Mathf.Sqrt (deltaX * deltaX + deltaY * deltaY), collisionMask)) {
								grounded = true; 
								deltaY = 0;
						}
		}

		Vector2 finalTrans = new Vector2 (deltaX, deltaY);

		transform.Translate(finalTrans);

	}

	public void setCollider(Vector3 size, Vector3 center)
	{
		collider.size = size; 
		collider.center = center; 

		s = size * colliderScale; 
		c = center * colliderScale; 
	}
}
